import request from '@/utils/request'; 
import { da } from 'element-plus/es/locales.mjs'; 
import { jwtDecode } from 'jwt-decode'; 

// 添加医疗记录接口
export function addMedicalRecord(data) {
    return request({
        url: '/medical-record/add',
        method: 'post',
        data: data
    }).then(response => {
        console.log('addMedicalRecord 接口响应:', response);
        if (response.code === "000") {
            return response;
        } else {
            console.error('addMedicalRecord 调用失败:', response.statusText);
            throw new Error(`非 000 状态码: ${response.status}`);
        }
    }).catch(error => {
        console.error('addMedicalRecord 请求失败:', error);
        throw error;
    });
}

// 删除医疗记录接口
export function deleteMedicalRecord(data) {
    return request({
        url: '/medical-record/delete',
        method: 'delete',
        data: data
    }).then(response => {
        console.log('deleteMedicalRecord 接口响应:', response);
        if (response.code === "000") {
            return response;
        } else {
            console.error('deleteMedicalRecord 调用失败:', response.statusText);
            throw new Error(`非 000 状态码: ${response.status}`);
        }
    }).catch(error => {
        console.error('deleteMedicalRecord 请求失败:', error);
        throw error;
    });
}

// 更新医疗记录接口
export function updateMedicalRecord(data) {
    return request({
        url: '/medical-record/update',
        method: 'put',
        data: data
    }).then(response => {
        console.log('updateMedicalRecord 接口响应:', response);
        if (response.code === "000") {
            return response;
        } else {
            console.error('updateMedicalRecord 调用失败:', response.statusText);
            throw new Error(`非 000 状态码: ${response.status}`);
        }
    }).catch(error => {
        console.error('updateMedicalRecord 请求失败:', error);
        throw error;
    });
}

// 获取医疗记录信息接口
export function getMedicalRecordInfo() {
    return request({
        url: '/medical-record/info',
        method: 'get',
        params: null
    }).then(response => {
        console.log('getMedicalRecordInfo 接口响应:', response);
        if (response.code === "000") {
            return response;
        } else {
            console.error('getMedicalRecordInfo 调用失败:', response.statusText);
            throw new Error(`非 000 状态码: ${response.status}`);
        }
    }).catch(error => {
        console.error('getMedicalRecordInfo 请求失败:', error);
        throw error;
    });
}

// 根据用户 ID 获取医疗记录列表接口
// 根据用户 ID 获取医疗记录列表接口
export function getMedicalRecordListByUserId(userId) {
    return request({
        url: '/medical-record/list-by-user-id',
        method: 'get',
        params: { userId }
    }).then(response => {
        console.log('getMedicalRecordListByUserId 接口响应:', response);
        if (response.code === "000") {
            return response; // 返回数据
        } else {
            console.error('getMedicalRecordListByUserId 调用失败:', response.statusText);
            throw new Error(`非 000 状态码: ${response.code}`);
        }
    }).catch(error => {
        console.error('getMedicalRecordListByUserId 请求失败:', error);
        throw error;
    });
}